﻿
using Red.Controls.Behaviors;
using System.Windows;
using System.Windows.Controls;

namespace Red.Controls.DataDriven
{
    /// <summary>
    ///     VectorEditor control responsible for handling all vector type data
    /// </summary>
    [InteropControl( "Vector3", "Vector" )]
    public partial class VectorEditor : UserControl
    {
        const double _narrowModeSize = 130;

        protected static readonly DependencyProperty NarrowModeProperty =
            DependencyProperty.Register( "NarrowMode", typeof( bool ), typeof( VectorEditor ) );
        
        protected bool NarrowMode
        {
            get { return (bool)GetValue( NarrowModeProperty ); }
            set { SetValue( NarrowModeProperty, value ); }
        }

        public VectorEditor()
        {
            InitializeComponent();
            Loaded += VectorEditor_Loaded;
            SizeChanged += VectorEditor_SizeChanged;
        }

        void VectorEditor_Loaded( object sender, RoutedEventArgs e )
        {
            UpdateNarrowMode();
        }

        private void VectorEditor_SizeChanged( object sender, SizeChangedEventArgs e )
        {
            UpdateNarrowMode();
        }

        private void UpdateNarrowMode()
        {
            NarrowMode = ActualWidth < _narrowModeSize;
        }
    }

    /// <summary>
    ///     Data-aware control for EulerAngles
    /// </summary>
    [InteropControl( "EulerAngles" )]
    public class EulerAnglesEditor : VectorEditor
    {
        public EulerAnglesEditor()
        {
            ToolTip = "Rotation|Represents an object rotation in Euler Angles|http://confluence:8090/display/CPT/Transform+control";

            _xLabel.Text = "P";
            _yLabel.Text = "R";
            _zLabel.Text = "Y";
            
            _xTextBox.SetValue( InteropExtension.PathProperty, "Pitch" );
            _yTextBox.SetValue( InteropExtension.PathProperty, "Roll" );
            _zTextBox.SetValue( InteropExtension.PathProperty, "Yaw" );

            _xTextBox.Suffix = "°";
            _yTextBox.Suffix = "°";
            _zTextBox.Suffix = "°";

            _xTextBox.Min = -360f;
            _yTextBox.Min = -360f;
            _zTextBox.Min = -360f;

            _xTextBox.Max = 360f;
            _yTextBox.Max = 360f;
            _zTextBox.Max = 360f;
        }
    }

    /// <summary>
    ///     Data-aware control for node Position
    /// </summary>
    [InteropControl( "Position3" )]
    public class PositionEditor : VectorEditor
    {
        public PositionEditor()
        {
            ToolTip = "Position|Represents an object position in meters";
            _xTextBox.Suffix = " m";
            _yTextBox.Suffix = " m";
            _zTextBox.Suffix = " m";

            _xTextBox.XUpdateModifier = ValueUpdateModifier.Hundredths;
            _xTextBox.YUpdateModifier = ValueUpdateModifier.Decimal;

            _yTextBox.XUpdateModifier = ValueUpdateModifier.Hundredths;
            _yTextBox.YUpdateModifier = ValueUpdateModifier.Decimal;

            _zTextBox.XUpdateModifier = ValueUpdateModifier.Hundredths;
            _zTextBox.YUpdateModifier = ValueUpdateModifier.Decimal;

            _xTextBox.MinDigitsAfterComma = 2;
            _yTextBox.MinDigitsAfterComma = 2;
            _zTextBox.MinDigitsAfterComma = 2;
        }
    }

    /// <summary>
    ///     Data-aware control for Scale
    /// </summary>
    [InteropControl( "Scale3" )]
    public class ScaleEditor : VectorEditor
    {
        public ScaleEditor()
        {
            ToolTip = "|Represents an object scale in meters|http://confluence:8090/display/CPT/Transform+control";
            _xTextBox.Min = 0.0001f;
            _yTextBox.Min = 0.0001f;
            _zTextBox.Min = 0.0001f;

            _xTextBox.XUpdateModifier = ValueUpdateModifier.Hundredths;
            _xTextBox.YUpdateModifier = ValueUpdateModifier.Decimal;

            _yTextBox.XUpdateModifier = ValueUpdateModifier.Hundredths;
            _yTextBox.YUpdateModifier = ValueUpdateModifier.Decimal;

            _zTextBox.XUpdateModifier = ValueUpdateModifier.Hundredths;
            _zTextBox.YUpdateModifier = ValueUpdateModifier.Decimal;

            _xTextBox.MinDigitsAfterComma = 2;
            _yTextBox.MinDigitsAfterComma = 2;
            _zTextBox.MinDigitsAfterComma = 2;
        }
    }
}
